package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * ClassName EmployeeServiceImpl
 * Create by Mike
 * Date 2021/8/1 16:46
 */
@Service
@Transactional
@Setter
@Slf4j
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;



    @Override
    public void save(Employee employee, Long[] roleIds) {

        //获取随机数uuid
        String uuid = UUID.randomUUID().toString();
        //截取随机数前五位
        String salt = uuid.substring(0, 5);
        //给明文指定算法
        String password = new Md5Hash(employee.getPassword(), salt, 2).toString();
        //将传入的密码和盐保存到数据库
        employee.setSalt(salt);
        employee.setPassword(password);
        employeeMapper.insert(employee);
        //除了往employee表插入数据
        //还要往中间表employee_role表中插入数据
        if (roleIds != null && roleIds.length > 0){
            for (Long roleId : roleIds) {
                employeeMapper.saveRelation(employee.getId(),roleId);
            }
        }
    }

    @Override
    public void delete(Long id) {
        //先到中间表区删除关系数据
        employeeMapper.deleteRelation(id);
        //再删除员工数据
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Employee employee, Long[] roleIds) {
        //先根据传入的信息修改员工信息
        employeeMapper.updateByPrimaryKey(employee);
        //删除employee_role表中旧有的关系数据
        employeeMapper.deleteRelation(employee.getId());
        //再插入新的关系数据
        if (roleIds != null && roleIds.length > 0) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {

        //使用分页插件,传入当前页和每页的数量
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        List<Employee> employees = employeeMapper.selectForList(qo);

        return new PageInfo(employees);
    }

    @Override
    public boolean existUsername(String username) {
        //employee username 有或者没有
        Employee employee =  employeeMapper.selectByUsername(username);
        return employee != null;
    }

    @Override
    public Employee login(String username, String password) {
        return employeeMapper.login(username,password);
    }

    @Override
    public Employee queryByUsername(String username) {
        return employeeMapper.selectByUsername(username);
    }
}
